package org.correct.correcter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

import org.correct.servlet.Initializer;
import org.correct.util.Queue;


public class Correcter {

	public static BKTree treeBoth;	
	public static ArrayList<String> stopwords;
	
	public Correcter()
	{
		
		//English
	    stopwords = new ArrayList<String>();
		try
		{
			FileReader stoptxt = new FileReader(Initializer.basePath + "dic/stopwords");
			//FileReader stoptxt = new FileReader("./dic/stopwords");
			BufferedReader br = new BufferedReader(stoptxt);
			String ts;
			while((ts = br.readLine()) != null)
			{
				stopwords.add(ts);
				//System.out.println(ts);
			}
			br.close();
			stoptxt.close();		
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
		
		//both
		 
	    treeBoth = new BKTree();
		
		ArrayList<String[]> wordsB = new ArrayList<String[]>();
		try
		{
			File d = new File(Initializer.basePath + "data/dic");
			//File d = new File("./data/dic");
			File [] list = d.listFiles();
			for(int i = 0; i < list.length; i++)
			{
				FileReader fr = new FileReader(list[i]);
				BufferedReader br = new BufferedReader(fr);
				String ts;
				while((ts = br.readLine()) != null)
				{
					String [] temp = new String[2];
					temp[0] = ts.split(" ")[0];
					temp[1] = ts.split(" ")[1];
					wordsB.add(temp);
					//System.out.println(temp[0]);
				}
				br.close();
				fr.close();
			}		
			
		}
		
		catch (Exception e)
		{
			
		}
		for(int i = 0; i < wordsB.size(); i++)
		{
			String[] temp = wordsB.get(i);
			if(String.valueOf(temp[0]).matches("[\\u4E00-\\u9FA5]+"))
			{
				//System.out.println(temp[0]+"Chinese");
				treeBoth.InsertHanyu(PinyinChange.changeToPinYin(temp[0]),temp[0],Integer.parseInt(temp[1]));
			}
			else
			{
				//System.out.println(temp[0]+"English");
				treeBoth.Insert(temp[0], Integer.parseInt(temp[1]));
			}
		}
		wordsB.clear();
		//System.out.println("insert tree finish");
	}
	public static ResultNode getSearchList(String content)
	{
		ResultNode node = new ResultNode();
		ArrayList<String> result = new ArrayList<String>();
		ArrayList<String> temp = new ArrayList<String>();
		
		//System.out.println(content+"++++content");
		temp = IKAnalysis.getWords(content);
		//System.out.println(temp+"+++++fenci jieguo");
		
		//去掉stopwords
		for(int i = 0; i < temp.size(); i++)
		{
			if(stopwords.contains(temp.get(i)))
				temp.remove(i);
		}
		
		
		for(int i = 0; i < temp.size(); i++)
		{
			char[] word = temp.get(i).toCharArray();
			//����Ϊ����
			if(String.valueOf(word[0]).matches("[\\u4E00-\\u9FA5]+"))
			{
				String hanyu = temp.get(i);
				//System.out.println("begin");
				String pinyin = PinyinChange.changeToPinYin(hanyu);
				//System.out.println(pinyin);
				ArrayList<String> hanyuTemp;				
				hanyuTemp = treeBoth.Search(pinyin, hanyu);
				//����Ȩֵ����
				result.add(getPopular(hanyuTemp,hanyu));
				//System.out.println("limian:"+getPopular(hanyuTemp,hanyu));
			}
			else
			{
				ArrayList<String> engTemp = new ArrayList<String>();
				//System.out.println("word:"+temp.get(i));
				engTemp = treeBoth.Search(temp.get(i));
				
				//
				result.add(getPopular(engTemp,temp.get(i)));
				//System.out.println("limian:"+getPopular(engTemp,temp.get(i)));
			}
		}
		node.result = result;
		int notHave = 0;
		for(int i = 0; i < temp.size(); i++)
		{
			if(!result.contains(temp.get(i)))
				notHave++;
		}
		float bit = (float)notHave/(float)(temp.size());
		if(bit > 0.1)
		{
			node.haveError = 1;
		}
		else
		{
			node.haveError = 0;
		}	
		node.keywords = temp;
		//System.out.println("correct finish");
		return node;
	}
	
	public static String getPopular(ArrayList<String> list,String beWord)
	{
		int max = 0;
		String result = "";
		for(int i = 0; i < list.size(); i+=3)
		{
			String word = list.get(i);
			int population = Integer.parseInt(list.get(i+1));
			int distance = Integer.parseInt(list.get(i+2));
			if((population * 1 + distance * 10)> max)
			{
				max = population * 1 + distance * 10;
				result = word;
			}
			if(word.equals(beWord))
			{
				result = beWord;
				return result;
			}
		}
		return result;
	}
	
	public static void closeCorrector()
	{
		Queue<BKNode> queue1 = new Queue<BKNode>();
		ArrayList<String> result = new ArrayList<String>();
		
		BKNode root = treeBoth.tree;
		
		queue1.add(root);
		
		//BKNode root2 = treeC.tree;
		//queue2.add(root2);
		
		int fileSize = 199;
		int sizeNow = 0;
		int fileNum = 0;
		//File temp;
		FileWriter fw;		
		
		//System.out.println(queue1.size());
			//root = queue1.poll();
			while(queue1.size() != 0)
			{
				root = queue1.poll();
				if(root.property == 0)
				{
					String content = "";
					content += root.word;
					content += " ";
					content += root.population;
					content += "\n";
					result.add(content);
					sizeNow++;
					if(sizeNow == fileSize+1)
					{
						sizeNow = 0;
						
						try
						{
							String tempPath = Initializer.basePath + "data/dic/Test/_"+String.valueOf(fileNum);
							fw = new FileWriter(tempPath);
							for(int j = 0; j < result.size(); j++)
							{
								fw.write(result.get(j));
							}
							//System.out.println("close"+fileNum);
							result.clear();
							fw.close();
						}
						catch (Exception e)
						{
							System.out.println(e);
						}
						fileNum++;
					}
				}
				else
				{
					for(int i = 0; i < root.hanyu.size(); i++)
					{
						String content = "";
						content += root.hanyu.get(i)[0];
						content += " ";
						content += root.hanyu.get(i)[1];
						content += "\n";
						result.add(content);
						sizeNow++;
						if(sizeNow == fileSize+1)
						{
							sizeNow = 0;
							
							try
							{
								String tempPath = Initializer.basePath + "data/dic/Test/_"+String.valueOf(fileNum);
								fw = new FileWriter(tempPath);
								for(int j = 0; j < result.size(); j++)
								{
									fw.write(result.get(j));
								}
								result.clear();
								//System.out.println("close"+fileNum);
								fw.close();
							}
							catch (Exception e)
							{
								System.out.println(e);
							}
							fileNum++;
						}
					}
				}
				//System.out.println("add begin");
				for(int i = 0; i < root.sons.size(); i++)
				{
					queue1.add(root.sons.get(i));
				}
				if(queue1.size() == 0)
				{
					try
					{
						String tempPath = Initializer.basePath + "data/dic/Test/_"+String.valueOf(fileNum);
						fw = new FileWriter(tempPath);		
						for(int j = 0; j < result.size(); j++)
						{
							fw.write(result.get(j));
						}
						result.clear();
						//System.out.println("close"+fileNum);
						fw.close();
					}
					catch (Exception e)
					{
						System.out.println(e);
					}
				}
			}
			
		
			
	}
}
